using System;
using UnityEngine;

public class CollisionSoundEmitter : MonoBehaviour
{
	public AudioClip sound;

	public bool playOnCollision = true;

	public bool playOnAngularVelocityChange;

	public float soundScale = 1f;

	private AudioSource soundSource;

	private Vector3 prevVelocity = Vector3.zero;

	private Vector3 prevAngularVelocity = Vector3.zero;

	private void Start()
	{
		soundSource = base.gameObject.AddComponent<AudioSource>();
		soundSource.clip = sound;
		soundSource.loop = false;
		soundSource.rolloffMode = AudioRolloffMode.Linear;
		soundSource.maxDistance = 30f;
		soundSource.dopplerLevel = 0.1f;
	}

	private void OnCollisionEnter()
	{
		if (Game.gameState == GameState.Playing && playOnCollision)
		{
			float num = (base.GetComponent<Rigidbody>().velocity - prevVelocity).sqrMagnitude * 0.005f;
			num *= soundScale;
			if (soundSource == null)
			{
				Debug.Log("WTF: CollisionSoundEmitter soundSource = null");
			}
			else
			{
				Audio.PlayShotSound(sound, num, soundSource);
			}
		}
	}

	private void FixedUpdate()
	{
		if (base.GetComponent<Rigidbody>() == null)
		{
			return;
		}
		if (playOnAngularVelocityChange && Game.gameState == GameState.Playing)
		{
			float magnitude = (base.GetComponent<Rigidbody>().angularVelocity - prevAngularVelocity).magnitude;
			if (magnitude > 0.8f)
			{
				float sqrMagnitude = (base.GetComponent<Rigidbody>().velocity - prevVelocity).sqrMagnitude;
				float num = sqrMagnitude * 0.001f * (float)Math.PI;
				num *= soundScale;
				Audio.PlayShotSound(sound, num, soundSource);
			}
		}
		prevVelocity = base.GetComponent<Rigidbody>().velocity;
		prevAngularVelocity = base.GetComponent<Rigidbody>().angularVelocity;
	}
}
